package cn.powerone.scm.controller;

import java.util.List;

import cn.powerone.scm.domain.*;
import cn.powerone.scm.service.*;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import cn.powerone.common.annotation.Log;
import cn.powerone.common.core.controller.BaseController;
import cn.powerone.common.core.domain.AjaxResult;
import cn.powerone.common.enums.BusinessType;
import cn.powerone.common.utils.poi.ExcelUtil;
import cn.powerone.common.core.page.TableDataInfo;

/**
 * 全局附件Controller
 * 
 * @author PowerOne
 * @date 2025-08-22
 */
@RestController
@RequestMapping("/scm/scmAttachmentLinkage")
@RequiredArgsConstructor
public class ScmAttachmentLinkageController extends BaseController
{

    private final IScmAttachmentLinkageService scmAttachmentLinkageService;
    private final IScmProjectsService scmProjectsService;
    private final IScmCustomerOrdersService scmCustomerOrdersService;
    private final IScmSupplierOrdersService scmSupplierOrdersService;
    private final IScmCustomerService scmCustomerService;
    private final IScmSupplierService scmSupplierService;

    /**
     * 查询全局附件列表
     */
    @PreAuthorize("@ss.hasPermi('scm:scmAttachmentLinkage:list')")
    @GetMapping("/list")
    public TableDataInfo list(ScmAttachmentLinkage scmAttachmentLinkage)
    {
        startPage();
        List<ScmAttachmentLinkage> list = scmAttachmentLinkageService.selectScmAttachmentLinkageList(scmAttachmentLinkage);
        return getDataTable(list);
    }

    /**
     * 导出全局附件列表
     */
    @PreAuthorize("@ss.hasPermi('scm:scmAttachmentLinkage:export')")
    @Log(title = "全局附件", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ScmAttachmentLinkage scmAttachmentLinkage)
    {
        List<ScmAttachmentLinkage> list = scmAttachmentLinkageService.selectScmAttachmentLinkageList(scmAttachmentLinkage);
        ExcelUtil<ScmAttachmentLinkage> util = new ExcelUtil<ScmAttachmentLinkage>(ScmAttachmentLinkage.class);
        util.exportExcel(response, list, "全局附件数据");
    }

    /**
     * 获取全局附件详细信息
     */
    @PreAuthorize("@ss.hasPermi('scm:scmAttachmentLinkage:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(scmAttachmentLinkageService.selectScmAttachmentLinkageById(id));
    }

    /**
     * 新增全局附件
     */
    @PreAuthorize("@ss.hasPermi('scm:scmAttachmentLinkage:add')")
    @Log(title = "全局附件", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ScmAttachmentLinkage scmAttachmentLinkage)
    {
        // 如果displayCode 为空，则使用根据规则组装，attachmentType 1-10，使用projectCode, 11-20，使用customerOrderCode, 21-30，使用supplierOrderCode, 31-40，使用customerName, 41-50，使用SupplierName
        if (scmAttachmentLinkage.getDisplayCode() == null || scmAttachmentLinkage.getDisplayCode().isEmpty()) {
            Long attachmentType = scmAttachmentLinkage.getAttachmentType();
            if (attachmentType >= 1 && attachmentType <= 10) {
                ScmProjects scmProjects = scmProjectsService.selectScmProjectsByProjectId(scmAttachmentLinkage.getLinkedId());
                scmAttachmentLinkage.setDisplayCode(scmProjects.getProjectCode());
            } else if (attachmentType >= 11 && attachmentType <= 20) {
                ScmCustomerOrders scmCustomerOrders = scmCustomerOrdersService.selectScmCustomerOrdersById(scmAttachmentLinkage.getLinkedId());
                scmAttachmentLinkage.setDisplayCode(scmCustomerOrders.getOrderCode());
            } else if (attachmentType >= 21 && attachmentType <= 30) {
                ScmSupplierOrders scmSupplierOrders = scmSupplierOrdersService.selectScmSupplierOrdersById(scmAttachmentLinkage.getLinkedId());
                scmAttachmentLinkage.setDisplayCode(scmSupplierOrders.getOrderCode());
            } else if (attachmentType >= 31 && attachmentType <= 40) {
                ScmCustomer scmCustomer = scmCustomerService.selectScmCustomerById(scmAttachmentLinkage.getLinkedId());
                scmAttachmentLinkage.setDisplayCode(scmCustomer.getName());
            } else if (attachmentType >= 41 && attachmentType <= 50) {
                ScmSupplier scmSupplier = scmSupplierService.selectScmSupplierById(scmAttachmentLinkage.getLinkedId());
                scmAttachmentLinkage.setDisplayCode(scmSupplier.getName());
            }
        }
        return toAjax(scmAttachmentLinkageService.insertScmAttachmentLinkage(scmAttachmentLinkage));
    }

    /**
     * 修改全局附件
     */
    @PreAuthorize("@ss.hasPermi('scm:scmAttachmentLinkage:edit')")
    @Log(title = "全局附件", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ScmAttachmentLinkage scmAttachmentLinkage)
    {
        return toAjax(scmAttachmentLinkageService.updateScmAttachmentLinkage(scmAttachmentLinkage));
    }

    /**
     * 删除全局附件
     */
    @PreAuthorize("@ss.hasPermi('scm:scmAttachmentLinkage:remove')")
    @Log(title = "全局附件", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(scmAttachmentLinkageService.deleteScmAttachmentLinkageByIds(ids));
    }
}
